//
//  LGMessageViewController.m
//  ligenyun
//
//  Created by Mac on 2018/3/24.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "LGMessageViewController.h"
#import "LGChartMessageTableViewCell.h"
#import "LGSystemMessageTableViewCell.h"
#import "LGSystemDetailViewController.h"
@interface LGMessageViewController ()
@property(nonatomic, strong) UISegmentedControl                     *segement;
@end

@implementation LGMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息";
    self.view.backgroundColor = TM_backgroundColor;
    [self configSubView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    CGFloat segementWith = (UIScreenWidth - 30)/3;
    self.segement = [[UISegmentedControl alloc] initWithItems:@[@"亲友消息",@"系统消息"]];
    self.segement.frame = CGRectMake(15, 20 + [TMUtils navgationBarBootom], segementWith * 3, 30);
    self.segement.tintColor = TM_redColor;
    self.segement.selectedSegmentIndex = 0;
    [self.segement setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} forState:UIControlStateSelected];
    [self.segement setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} forState:UIControlStateNormal];
    [self.segement addTarget:self action:@selector(onSelctedSegement:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segement];
    
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.frame = CGRectMake(0, self.segement.bottom + 15, UIScreenWidth, [TMUtils tabBarTop] - self.segement.bottom -15);
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无消息" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self judgeSendWhatRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    [self.tableView registerClass:[LGChartMessageTableViewCell class] forCellReuseIdentifier:@"chatMessage"];
    [self.tableView registerClass:[LGSystemMessageTableViewCell class] forCellReuseIdentifier:@"systemMessage"];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [LGChartMessageTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.segement.selectedSegmentIndex == 0) {
        LGChartMessageTableViewCell * cell  = [self.tableView dequeueReusableCellWithIdentifier:@"chatMessage"];
        return  cell;
    }else{
        LGSystemMessageTableViewCell * cell  = [self.tableView dequeueReusableCellWithIdentifier:@"systemMessage"];
        return  cell;
    }
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.segement.selectedSegmentIndex == 1) {
        LGSystemDetailViewController * vc = [LGSystemDetailViewController new];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        
    }

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onSelctedSegement:(UISegmentedControl*)segement{
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark --NetWork Method
- (void)judgeSendWhatRequest{
    if (self.segement.selectedSegmentIndex == 0) {
        [self fetchMessageList];
    }else{
        [self sendSystemMessageListRequest];
    }
}
- (void)fetchMessageList{
    [TMUtils debugSimulationNetWorkWithHadle:^(NSInteger afterTime) {
        [self.tableView.mj_header endRefreshing];
         [self.tableView reloadData];
    }];
}
- (void)sendSystemMessageListRequest{
    [TMUtils debugSimulationNetWorkWithHadle:^(NSInteger afterTime) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    }];
}
@end
